<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>png++: writer.hpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">png++
   &#160;<span id="projectnumber">0.2.1</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">writer.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<a href="writer_8hpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * Copyright (C) 2007,2008   Alex Shulgin</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * This file is part of png++ the C++ wrapper for libpng.  PNG++ is free</span>
<a name="l00005"></a>00005 <span class="comment"> * software; the exact copying conditions are as follows:</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<a name="l00008"></a>00008 <span class="comment"> * modification, are permitted provided that the following conditions are met:</span>
<a name="l00009"></a>00009 <span class="comment"> *</span>
<a name="l00010"></a>00010 <span class="comment"> * 1. Redistributions of source code must retain the above copyright notice,</span>
<a name="l00011"></a>00011 <span class="comment"> * this list of conditions and the following disclaimer.</span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright</span>
<a name="l00014"></a>00014 <span class="comment"> * notice, this list of conditions and the following disclaimer in the</span>
<a name="l00015"></a>00015 <span class="comment"> * documentation and/or other materials provided with the distribution.</span>
<a name="l00016"></a>00016 <span class="comment"> *</span>
<a name="l00017"></a>00017 <span class="comment"> * 3. The name of the author may not be used to endorse or promote products</span>
<a name="l00018"></a>00018 <span class="comment"> * derived from this software without specific prior written permission.</span>
<a name="l00019"></a>00019 <span class="comment"> *</span>
<a name="l00020"></a>00020 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS&#39;&#39; AND ANY EXPRESS OR</span>
<a name="l00021"></a>00021 <span class="comment"> * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES</span>
<a name="l00022"></a>00022 <span class="comment"> * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN</span>
<a name="l00023"></a>00023 <span class="comment"> * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00024"></a>00024 <span class="comment"> * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED</span>
<a name="l00025"></a>00025 <span class="comment"> * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR</span>
<a name="l00026"></a>00026 <span class="comment"> * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF</span>
<a name="l00027"></a>00027 <span class="comment"> * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING</span>
<a name="l00028"></a>00028 <span class="comment"> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span>
<a name="l00029"></a>00029 <span class="comment"> * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00030"></a>00030 <span class="comment"> */</span>
<a name="l00031"></a>00031 <span class="preprocessor">#ifndef PNGPP_WRITER_HPP_INCLUDED</span>
<a name="l00032"></a>00032 <span class="preprocessor"></span><span class="preprocessor">#define PNGPP_WRITER_HPP_INCLUDED</span>
<a name="l00033"></a>00033 <span class="preprocessor"></span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &quot;<a class="code" href="io__base_8hpp.html">io_base.hpp</a>&quot;</span>
<a name="l00036"></a>00036 
<a name="l00037"></a>00037 <span class="keyword">namespace </span>png
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039 
<a name="l00065"></a>00065     <span class="keyword">template</span>&lt; <span class="keyword">class</span> ostream &gt;
<a name="l00066"></a><a class="code" href="classpng_1_1writer.html">00066</a>     <span class="keyword">class </span><a class="code" href="classpng_1_1writer.html" title="PNG writer class template. This is the low-level writing interface--use image class or generator clas...">writer</a>
<a name="l00067"></a>00067         : <span class="keyword">public</span> <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>
<a name="l00068"></a>00068     {
<a name="l00069"></a>00069     <span class="keyword">public</span>:
<a name="l00074"></a><a class="code" href="classpng_1_1writer.html#a4f29f0237affe2660f068851403ca468">00074</a>         <span class="keyword">explicit</span> <a class="code" href="classpng_1_1writer.html#a4f29f0237affe2660f068851403ca468" title="Constructs a writer prepared to write PNG image into a stream.">writer</a>(ostream&amp; stream)
<a name="l00075"></a>00075             : <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>(png_create_write_struct(PNG_LIBPNG_VER_STRING,
<a name="l00076"></a>00076                                               static_cast&lt; <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>* &gt;(this),
<a name="l00077"></a>00077                                               <a class="code" href="classpng_1_1io__base.html#ad01b0dbb31b81fe9d6de1d9ff2f2136e">raise_error</a>,
<a name="l00078"></a>00078                                               0))
<a name="l00079"></a>00079         {
<a name="l00080"></a>00080             png_set_write_fn(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>, &amp; stream, write_data, flush_data);
<a name="l00081"></a>00081         }
<a name="l00082"></a>00082 
<a name="l00083"></a><a class="code" href="classpng_1_1writer.html#a75ec970309438fd2cf5166fe259de36e">00083</a>         <a class="code" href="classpng_1_1writer.html#a75ec970309438fd2cf5166fe259de36e">~writer</a>()
<a name="l00084"></a>00084         {
<a name="l00085"></a>00085             <a class="code" href="classpng_1_1io__base.html#a4dc235d3aa8764d1a1a73c6d1fefed61">m_end_info</a>.<a class="code" href="classpng_1_1end__info.html#a40da9911dddbf4ec026aa8480800a755">destroy</a>();
<a name="l00086"></a>00086             png_destroy_write_struct(&amp; <a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>, <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info__base.html#afe70c0d333349a9b5fc7a1d046fc3fbc">get_png_info_ptr</a>());
<a name="l00087"></a>00087         }
<a name="l00088"></a>00088 
<a name="l00089"></a><a class="code" href="classpng_1_1writer.html#a9804465c1d54eec114a38e3d29a4481d">00089</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1writer.html#a9804465c1d54eec114a38e3d29a4481d">write_png</a>()<span class="keyword"> const</span>
<a name="l00090"></a>00090 <span class="keyword">        </span>{
<a name="l00091"></a>00091             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00092"></a>00092             {
<a name="l00093"></a>00093                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00094"></a>00094             }
<a name="l00095"></a>00095             png_write_png(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>,
<a name="l00096"></a>00096                           <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info__base.html#a030a8262a2ef1bf8c97a2eafe71c2f3c">get_png_info</a>(),
<a name="l00097"></a>00097                           <span class="comment">/* transforms = */</span> 0,
<a name="l00098"></a>00098                           <span class="comment">/* params = */</span> 0);
<a name="l00099"></a>00099         }
<a name="l00100"></a>00100 
<a name="l00104"></a><a class="code" href="classpng_1_1writer.html#a36bc62c0fe6d99dcec439fde2d54228c">00104</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1writer.html#a36bc62c0fe6d99dcec439fde2d54228c" title="Write info about PNG image.">write_info</a>()<span class="keyword"> const</span>
<a name="l00105"></a>00105 <span class="keyword">        </span>{
<a name="l00106"></a>00106             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00107"></a>00107             {
<a name="l00108"></a>00108                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00109"></a>00109             }
<a name="l00110"></a>00110             <a class="code" href="classpng_1_1io__base.html#a3253298a348305da3f3b6cf38f201300">m_info</a>.<a class="code" href="classpng_1_1info.html#a863333fbef670b244920022d1a0ee210">write</a>();
<a name="l00111"></a>00111         }
<a name="l00112"></a>00112 
<a name="l00116"></a><a class="code" href="classpng_1_1writer.html#aa88beb256941e06e54e6842a1b0a81ee">00116</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1writer.html#aa88beb256941e06e54e6842a1b0a81ee" title="Writes a row of image data at a time.">write_row</a>(<a class="code" href="namespacepng.html#ac0e0e2e09bdd9a287618bfdc5f31ca52">byte</a>* bytes)
<a name="l00117"></a>00117         {
<a name="l00118"></a>00118             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00119"></a>00119             {
<a name="l00120"></a>00120                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00121"></a>00121             }
<a name="l00122"></a>00122             png_write_row(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>, bytes);
<a name="l00123"></a>00123         }
<a name="l00124"></a>00124 
<a name="l00128"></a><a class="code" href="classpng_1_1writer.html#a935ff64a683bb7d98961ff936976f52c">00128</a>         <span class="keywordtype">void</span> <a class="code" href="classpng_1_1writer.html#a935ff64a683bb7d98961ff936976f52c" title="Reads ending info about PNG image.">write_end_info</a>()<span class="keyword"> const</span>
<a name="l00129"></a>00129 <span class="keyword">        </span>{
<a name="l00130"></a>00130             <span class="keywordflow">if</span> (setjmp(png_jmpbuf(<a class="code" href="classpng_1_1io__base.html#a343eb5ab2ad984e7020d8489b64ec9d2">m_png</a>)))
<a name="l00131"></a>00131             {
<a name="l00132"></a>00132                 <span class="keywordflow">throw</span> <a class="code" href="classpng_1_1error.html" title="Exception class to represent runtime errors related to png++ operation.">error</a>(<a class="code" href="classpng_1_1io__base.html#a0525d690575be17366b555672bac823e">m_error</a>);
<a name="l00133"></a>00133             }
<a name="l00134"></a>00134             <a class="code" href="classpng_1_1io__base.html#a4dc235d3aa8764d1a1a73c6d1fefed61">m_end_info</a>.<a class="code" href="classpng_1_1end__info.html#af3974f800aa7987beef61667313ab3ad">write</a>();
<a name="l00135"></a>00135         }
<a name="l00136"></a>00136 
<a name="l00137"></a>00137     <span class="keyword">private</span>:
<a name="l00138"></a>00138         <span class="keyword">static</span> <span class="keywordtype">void</span> write_data(png_struct* png, <a class="code" href="namespacepng.html#ac0e0e2e09bdd9a287618bfdc5f31ca52">byte</a>* data, <span class="keywordtype">size_t</span> length)
<a name="l00139"></a>00139         {
<a name="l00140"></a>00140             <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>* io = <span class="keyword">static_cast&lt;</span> <a class="code" href="classpng_1_1io__base.html" title="Base class for PNG reader/writer classes.">io_base</a>* <span class="keyword">&gt;</span>(png_get_error_ptr(png));
<a name="l00141"></a>00141             <a class="code" href="classpng_1_1writer.html" title="PNG writer class template. This is the low-level writing interface--use image class or generator clas...">writer</a>* wr = <span class="keyword">static_cast&lt;</span> <a class="code" href="classpng_1_1writer.html" title="PNG writer class template. This is the low-level writing interface--use image class or generator clas...">writer</a>* <span class="keyword">&gt;</span>(io);
<a name="l00142"></a>00142             wr-&gt;<a class="code" href="classpng_1_1io__base.html#afba999be609454b12e2cd4ef3ca9a3f2">reset_error</a>();
<a name="l00143"></a>00143             ostream* stream = <span class="keyword">reinterpret_cast&lt;</span> ostream* <span class="keyword">&gt;</span>(png_get_io_ptr(png));
<a name="l00144"></a>00144             <span class="keywordflow">try</span>
<a name="l00145"></a>00145             {
<a name="l00146"></a>00146                 stream-&gt;write(reinterpret_cast&lt; char* &gt;(data), length);
<a name="l00147"></a>00147                 <span class="keywordflow">if</span> (!stream-&gt;good())
<a name="l00148"></a>00148                 {
<a name="l00149"></a>00149                     wr-&gt;<a class="code" href="classpng_1_1io__base.html#a39065eb7d408ba2c251d381095dade8a">set_error</a>(<span class="stringliteral">&quot;ostream::write() failed&quot;</span>);
<a name="l00150"></a>00150                 }
<a name="l00151"></a>00151             }
<a name="l00152"></a>00152             <span class="keywordflow">catch</span> (std::exception <span class="keyword">const</span>&amp; error)
<a name="l00153"></a>00153             {
<a name="l00154"></a>00154                 wr-&gt;<a class="code" href="classpng_1_1io__base.html#a39065eb7d408ba2c251d381095dade8a">set_error</a>(error.what());
<a name="l00155"></a>00155             }
<a name="l00156"></a>00156             <span class="keywordflow">catch</span> (...)
<a name="l00157"></a>00157             {
<a name="l00158"></a>00158                 assert(!<span class="stringliteral">&quot;caught something wrong&quot;</span>);
<a name="l00159"></a>00159                 wr-&gt;<a class="code" href="classpng_1_1io__base.html#a39065eb7d408ba2c251d381095dade8a">set_error</a>(<span class="stringliteral">&quot;write_data: caught something wrong&quot;</span>);
<a name="l00160"></a>00160             }
<a name="l00161"></a>00161             <span class="keywordflow">if</span> (wr-&gt;<a class="code" href="classpng_1_1io__base.html#a4bfbb79bd63e35d54339f15baade71f5">is_error</a>())
<a name="l00162"></a>00162             {
<a name="l00163"></a>00163                 wr-&gt;<a class="code" href="classpng_1_1io__base.html#ad01b0dbb31b81fe9d6de1d9ff2f2136e">raise_error</a>();
<a name="l00164"></a>00164             }
<a name="l00165"></a>00165         }
<a name="l00166"></a>00166 
<a name="l00167"></a>00167         <span class="keyword">static</span> <span class="keywordtype">void</span> flush_data(png_struct* png)
<a name="l00168"></a>00168         {
<a name="l00169"></a>00169             <a class="code" href="classpng_1_1io__base.html#a74af9c813a17cd2f62878545cb389c0e">io_base</a>* io = <span class="keyword">static_cast&lt;</span> <a class="code" href="classpng_1_1io__base.html#a74af9c813a17cd2f62878545cb389c0e">io_base</a>* <span class="keyword">&gt;</span>(png_get_error_ptr(png));
<a name="l00170"></a>00170             <a class="code" href="classpng_1_1writer.html#a4f29f0237affe2660f068851403ca468" title="Constructs a writer prepared to write PNG image into a stream.">writer</a>* wr = <span class="keyword">static_cast&lt;</span> <a class="code" href="classpng_1_1writer.html#a4f29f0237affe2660f068851403ca468" title="Constructs a writer prepared to write PNG image into a stream.">writer</a>* <span class="keyword">&gt;</span>(io);
<a name="l00171"></a>00171             wr-&gt;reset_error();
<a name="l00172"></a>00172             ostream* stream = <span class="keyword">reinterpret_cast&lt;</span> ostream* <span class="keyword">&gt;</span>(png_get_io_ptr(png));
<a name="l00173"></a>00173             <span class="keywordflow">try</span>
<a name="l00174"></a>00174             {
<a name="l00175"></a>00175                 stream-&gt;flush();
<a name="l00176"></a>00176                 <span class="keywordflow">if</span> (!stream-&gt;good())
<a name="l00177"></a>00177                 {
<a name="l00178"></a>00178                     wr-&gt;set_error(<span class="stringliteral">&quot;ostream::flush() failed&quot;</span>);
<a name="l00179"></a>00179                 }
<a name="l00180"></a>00180             }
<a name="l00181"></a>00181             <span class="keywordflow">catch</span> (std::exception <span class="keyword">const</span>&amp; error)
<a name="l00182"></a>00182             {
<a name="l00183"></a>00183                 wr-&gt;set_error(error.what());
<a name="l00184"></a>00184             }
<a name="l00185"></a>00185             <span class="keywordflow">catch</span> (...)
<a name="l00186"></a>00186             {
<a name="l00187"></a>00187                 assert(!<span class="stringliteral">&quot;caught something wrong&quot;</span>);
<a name="l00188"></a>00188                 wr-&gt;set_error(<span class="stringliteral">&quot;flush_data: caught something wrong&quot;</span>);
<a name="l00189"></a>00189             }
<a name="l00190"></a>00190             <span class="keywordflow">if</span> (wr-&gt;is_error())
<a name="l00191"></a>00191             {
<a name="l00192"></a>00192                 wr-&gt;raise_error();
<a name="l00193"></a>00193             }
<a name="l00194"></a>00194         }
<a name="l00195"></a>00195     };
<a name="l00196"></a>00196 
<a name="l00197"></a>00197 } <span class="comment">// namespace png</span>
<a name="l00198"></a>00198 
<a name="l00199"></a>00199 <span class="preprocessor">#endif // PNGPP_WRITER_HPP_INCLUDED</span>
</pre></div></div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated on Fri Jan 25 2013 14:10:10 for png++ by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
